package com.lindroy.androidplayer.ui.adapter

import android.widget.ImageView
import android.widget.TextView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.lindroy.androidplayer.R
import com.lindroy.androidplayer.extensions.loadUrl
import com.lindroy.androidplayer.logic.model.ProjectItemResp
import com.lindroy.lib_utils.extensions.getResString

/**
 * @author Lin
 * @date 2021/4/24
 * @function 项目列表适配器
 */
class ProjectListAdapter :
    BaseQuickAdapter<ProjectItemResp, BaseViewHolder>(R.layout.project_item) {

    private var collectListener: ((collect: Boolean, id: Int) -> Unit)? = null

    private var itemClickListener: ((item: ProjectItemResp) -> Unit)? = null

    override fun convert(holder: BaseViewHolder, item: ProjectItemResp) {
        val tvDesc = holder.getView<TextView>(R.id.tvDesc)
        val tvProjectName = holder.getView<TextView>(R.id.tvProjectName)
        tvProjectName.text = item.title
        tvProjectName.post {
            //如果标题已经显示两行了，那么简介只显示一行；该操作需要TextView绘制完毕
            tvDesc.maxLines = if (tvProjectName.lineCount >= 2) 1 else 2
        }
        tvDesc.text = item.desc
        holder.setText(R.id.tvDate, item.niceDate)
            .setText(R.id.tvAuthor, item.author)
            .setText(R.id.tvTag, getResString(R.string.from_tag, item.chapterName))
        holder.getView<ImageView>(R.id.ivEnvelope).loadUrl(item.envelopePic)
        holder.getView<ImageView>(R.id.ivCollect).also {
            it.setImageResource(item.collectedIconId)
            it.setOnClickListener {
                collectListener?.invoke(item.collect, item.id)
            }
        }
        holder.itemView.setOnClickListener {
            itemClickListener?.invoke(item)
        }
    }

    /**
     * 收藏按钮点击监听
     */
    fun setOnCollectClickListener(call: (collect: Boolean, id: Int) -> Unit) {
        collectListener = call
    }

    /**
     * Item点击事件
     */
    fun setOnItemClickListener(callback: (item: ProjectItemResp) -> Unit) {
        itemClickListener = callback
    }

}